<?php
class ProjectinfosController extends AppController {

	var $name = 'Projectinfos';

	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index', 'view', 'category');
	}

	function index() {		
		$this->Projectinfo->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array();
		
		////filter city id
		$cities = $this->Projectinfo->City->find('all');
		////$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		//if(!empty($this->params['form']['filter_city_id']))
		//	$city_id = $this->params['form']['filter_city_id'];
		//else
		//	$city_id = $this->Session->read("Projectinfo_city_id");
		//
		//$this->Session->write("Projectinfo_city_id", $city_id);
		//
		//if($city_id && $city_id != "all")
		//{
		//	$conditions["Projectinfo.city_id"] = $city_id;			
		//}
		//
		////echo $this->Session->read("districts_city_id");
		//
		////filter district id
		//$districts = $this->Projectinfo->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		//if(!empty($this->params['form']['filter_district_id']))
		//	$district_id = $this->params['form']['filter_district_id'];
		//else
		//	$district_id = $this->Session->read("Projectinfo_district_id");
		//
		//$this->Session->write("Projectinfo_district_id", $district_id);
		//
		//if($district_id && $district_id != "all")
		//{
		//	$conditions["Projectinfo.district_id"] = $district_id;			
		//}
		////echo $this->Session->read("districts_city_id");
		//
		////filter career id
		//$careers = $this->Projectinfo->Career->find('all');		
		//if(!empty($this->params['form']['filter_career_id']))
		//	$career_id = $this->params['form']['filter_career_id'];
		//else
		//	$career_id = $this->Session->read("Projectinfo_career_id");
		//
		//$this->Session->write("Projectinfo_career_id", $career_id);
		//
		//if($career_id && $career_id != "all")
		//{
		//	$conditions["Projectinfo.career_id"] = $career_id;			
		//}
		////echo $this->Session->read("districts_city_id");
		//
		////filter keyword
		//$keyword = $this->Session->read("Projectinfo_keyword");
		//if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] != '')
		//{
		//	$keyword = $this->params['form']['filter_keyword'];
		//	$this->Session->write("Projectinfo_keyword", $keyword);
		//}
		//if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] == '')
		//{
		//	$keyword = '';
		//	$this->Session->write("Projectinfo_keyword", $keyword);
		//}
		//
		//
		//
		//if($keyword && $keyword != "")
		//{
		//	$conditions["Projectinfo.name LIKE ?"] = '%'.$keyword.'%';
		//	//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
		//}
		//
		////var_dump($this->params);
		//if(count($conditions))
		//{
		//	$this->paginate = array(
		//		'conditions' => $conditions				
		//	);
		//}
		//
		//$Projectinfos = $this->paginate();
		//foreach($Projectinfos as $k => $item)
		//{
		//	//$Projectinfos[$k]["Projectinfo"]["price"] = $item["Projectinfo"]["price"] != '' ? number_format($item["Projectinfo"]["price"],0,",", "."): '';
		//	$Projectinfos[$k]["Projectinfo"]["create_date"] = date('d/m/Y', strtotime($item["Projectinfo"]["create_date"]));
		//	$Projectinfos[$k]["Projectinfo"]["name"] = parent::snippet($item["Projectinfo"]["name"], 40);
		//}
		
		$types = $this->Projectinfo->Type->find('all');
		foreach($types as $t => $item)
		{
			$types[$t]['products'] = $this->Projectinfo->find('all', array('conditions'=>array('Projectinfo.type_id'=>$item['Type']['id']), 'limit'=>10, 'order'=>'create_date DESC'));
			foreach($types[$t]['products'] as $k => $item)
			{
				//$Projectinfos[$k]["Projectinfo"]["price"] = $item["Projectinfo"]["price"] != '' ? number_format($item["Projectinfo"]["price"],0,",", "."): '';
				//$Projectinfos_vip[$k]["Projectinfo"]["create_date"] = date('d/m/Y', strtotime($item["Projectinfo"]["create_date"]));
				$types[$t]['products'][$k]["Projectinfo"]["name"] = parent::snippet($item["Projectinfo"]["name"], 40);
				//$Projectinfos_vip[$k]["Projectinfo"]["description"] = parent::snippet($item["Projectinfo"]["description"], 400);
				//$Projectinfos_vip[$k]["Projectinfo"]["image"] = parent::getImage($item["Projectinfo"]["description"]);
				
			}
			
		}
		
		$Projectinfos_vip = $this->Projectinfo->find('all', array('order'=>'Projectinfo.hit DESC', 'limit'=>10));
		foreach($Projectinfos_vip as $k => $item)
		{
			//$Projectinfos[$k]["Projectinfo"]["price"] = $item["Projectinfo"]["price"] != '' ? number_format($item["Projectinfo"]["price"],0,",", "."): '';
			$Projectinfos_vip[$k]["Projectinfo"]["create_date"] = date('d/m/Y', strtotime($item["Projectinfo"]["create_date"]));
			//$Projectinfos_vip[$k]["Projectinfo"]["name"] = parent::snippet($item["Projectinfo"]["name"], 40);
			$Projectinfos_vip[$k]["Projectinfo"]["description"] = parent::snippet($item["Projectinfo"]["description"], 800);
			$Projectinfos_vip[$k]["Projectinfo"]["image"] = parent::getImage($item["Projectinfo"]["description"]);
			
		}
		
		//$this->set('Projectinfos', $Projectinfos);
		$this->set('types', $types);
		$this->set('Projectinfos_vip', $Projectinfos_vip);
		$this->set('cities', $cities);
		//$this->set('city_id', $city_id);
		//$this->set('districts', $districts);		
		//$this->set('district_id', $district_id);
		//$this->set('careers', $careers);		
		//$this->set('career_id', $career_id);
		//$this->set('keyword', $keyword);	
	}
	
	function category($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Projectinfo Cat', true));
			$this->redirect(array('action' => 'index'));
		}
		
		$type = $this->Projectinfo->Type->read(null, $id);
		
		$this->Projectinfo->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array('Projectinfo.type_id'=>$id);
		
		//filter city id
		$cities = $this->Projectinfo->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Projectinfo_city_id");
		
		$this->Session->write("Projectinfo_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Projectinfo.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Projectinfo->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Projectinfo_district_id");
		
		$this->Session->write("Projectinfo_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Projectinfo.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Projectinfo->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Projectinfo_career_id");
		
		$this->Session->write("Projectinfo_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Projectinfo.career_id"] = $career_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter keyword
		$keyword = $this->Session->read("Projectinfo_keyword");
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] != '')
		{
			$keyword = $this->params['form']['filter_keyword'];
			$this->Session->write("Projectinfo_keyword", $keyword);
		}
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] == '')
		{
			$keyword = '';
			$this->Session->write("Projectinfo_keyword", $keyword);
		}
		
		
		
		if($keyword && $keyword != "")
		{
			$conditions["Projectinfo.name LIKE ?"] = '%'.$keyword.'%';
			//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
		}
		
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions, 'order'=>'create_date DESC'				
			);
		}
		
		$Projectinfos = $this->paginate();
		foreach($Projectinfos as $k => $item)
		{
			//$Projectinfos[$k]["Projectinfo"]["price"] = $item["Projectinfo"]["price"] != '' ? number_format($item["Projectinfo"]["price"],0,",", "."): '';
			$Projectinfos[$k]["Projectinfo"]["create_date"] = date('d/m/Y', strtotime($item["Projectinfo"]["create_date"]));
			$Projectinfos[$k]["Projectinfo"]["name"] = parent::snippet($item["Projectinfo"]["name"], 40);
		}
		
		$Projectinfos_vip = $this->Projectinfo->find('all', array('order'=>'Projectinfo.hit DESC', 'limit'=>10));
		foreach($Projectinfos_vip as $k => $item)
		{
			//$Projectinfos[$k]["Projectinfo"]["price"] = $item["Projectinfo"]["price"] != '' ? number_format($item["Projectinfo"]["price"],0,",", "."): '';
			$Projectinfos_vip[$k]["Projectinfo"]["create_date"] = date('d/m/Y', strtotime($item["Projectinfo"]["create_date"]));
			//$Projectinfos_vip[$k]["Projectinfo"]["name"] = parent::snippet($item["Projectinfo"]["name"], 40);
			$Projectinfos_vip[$k]["Projectinfo"]["description"] = parent::snippet($item["Projectinfo"]["description"], 800);
			$Projectinfos_vip[$k]["Projectinfo"]["image"] = parent::getImage($item["Projectinfo"]["description"]);
			
		}
		
		$this->set('Projectinfos', $Projectinfos);
		$this->set('Projectinfos_vip', $Projectinfos_vip);
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', $keyword);
		$this->set('type', $type);
	}
	
	function view($id = null) {
		$this->layout = 'home';
		if (!$id) {
			$this->Session->setFlash(__('Invalid Projectinfo', true));
			$this->redirect(array('action' => 'index'));
		}
		$Projectinfo = $this->Projectinfo->read(null, $id);
		$Projectinfo["Projectinfo"]["hit"] += 1;
		$this->Projectinfo->save($Projectinfo);			
		
		//$Projectinfo["Projectinfo"]["price"] = $Projectinfo["Projectinfo"]["price"] != '' ? number_format($Projectinfo["Projectinfo"]["price"],0,",", "."): '';
		$this->set('Projectinfo', $Projectinfo);
		
		
		$this->Projectinfo->recursive = 0;
		
		$conditions = array('Projectinfo.type_id'=>$Projectinfo["Type"]["id"]);
		
		//filter city id
		$cities = $this->Projectinfo->City->find('all');
		$city_id = $Projectinfo["Projectinfo"]["city_id"];		
		
		if($city_id && $city_id != "all")
		{
			$conditions["Projectinfo.city_id"] = $city_id;			
		}
		
		//filter district id
		$districts = $this->Projectinfo->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		$district_id = $Projectinfo["Projectinfo"]["district_id"];
		
		if($district_id && $district_id != "all")
		{
			$conditions["Projectinfo.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Projectinfo->Career->find('all');		
		$career_id = $Projectinfo["Projectinfo"]["career_id"];
		
		if($career_id && $career_id != "all")
		{
			$conditions["Projectinfo.career_id"] = $career_id;			
		}		

		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$this->set('Projectinfos', $this->paginate());
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', '');
	}
	
	function admin_index()
	{
		$this->Projectinfo->recursive = 0;
		
		$conditions = array();
		
		//filter city id
		$cities = $this->Projectinfo->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Projectinfo_city_id");
		
		$this->Session->write("Projectinfo_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Projectinfo.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Projectinfo->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Projectinfo_district_id");
		
		$this->Session->write("Projectinfo_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Projectinfo.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Projectinfo->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Projectinfo_career_id");
		
		$this->Session->write("Projectinfo_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Projectinfo.career_id"] = $career_id;			
		}
		
		////filter keyword		
		//if(!empty($this->params['form']['filter_keyword']))
		//	$keyword = $this->params['form']['filter_keyword'];
		//else
		//	$keyword = $this->Session->read("Projectinfo_keyword");
		//
		//$this->Session->write("Projectinfo_keyword", $keyword);
		//
		//if($keyword && $keyword != "")
		//{
		//	$conditions["Projectinfo.name LIKE ?"] = '%'.$keyword.'%';
		//	//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
		//}
		//
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$this->set('Projectinfos', $this->paginate());
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Projectinfo', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('Projectinfo', $this->Projectinfo->read(null, $id));
	}

	function admin_add() {
		$city_id = 1;
		$usr = $this->Auth->user();
		if (!empty($this->data)) {
			$this->Projectinfo->create();
			if ($this->Projectinfo->save($this->data)) {
				$this->Session->setFlash(__('The Projectinfo has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Projectinfo could not be saved. Please, try again.', true));
			}
			$city_id = $this->data["Projectinfo"]["city_id"];
		}
		$this->data["Projectinfo"]["user_id"] = $usr["User"]["id"];
		$users = $this->Projectinfo->User->find('list');
		$types = $this->Projectinfo->Type->find('list');
		$cities = $this->Projectinfo->City->find('list');
		$dits = $this->Projectinfo->District->find('list', array("conditions"=>array("city_id"=>$city_id)));
		$districts[0] = "- ".__('Quận/Huyện', true)." -";
		foreach($dits as $key => $value)
		{
			$districts[$key] = $value;
		}
		$cas = $this->Projectinfo->Career->find('list');
		$careers[0] = "- ".__('Ngành nghề', true)." -";
		foreach($cas as $key => $value)
		{
			$careers[$key] = $value;
		}
		$this->set(compact('users', 'cities', 'districts', 'careers', 'types'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Projectinfo', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Projectinfo->save($this->data)) {
				$this->Session->setFlash(__('The Projectinfo has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Projectinfo could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Projectinfo->read(null, $id);
		}
		$users = $this->Projectinfo->User->find('list');
		$cities = $this->Projectinfo->City->find('list');
		$types = $this->Projectinfo->Type->find('list');
		$dits = $this->Projectinfo->District->find('list', array("conditions"=>array("city_id"=>$this->data["Projectinfo"]["city_id"])));
		$districts[0] = "- ".__('Quận/Huyện', true)." -";
		foreach($dits as $key => $value)
		{
			$districts[$key] = $value;
		}
		$cas = $this->Projectinfo->Career->find('list');
		$careers[0] = "- ".__('Ngành nghề', true)." -";
		foreach($cas as $key => $value)
		{
			$careers[$key] = $value;
		}
		$this->set(compact('users', 'cities', 'districts', 'careers', 'types'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Projectinfo', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Projectinfo->delete($id)) {
			$this->Session->setFlash(__('Projectinfo deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Projectinfo was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
